/*
 * @Date: 2023-03-12 21:04:14
 * @LastEditors: Zireael 1193875164@qq.com
 * @LastEditTime: 2023-03-25 16:20:57
 * @FilePath: \A19\backend\src\api\Charts\ApiGetAllData.ts
 */
import { ApiCall } from "tsrpc";
import { ReqGetAllData, ResGetAllData } from "../../shared/protocols/Charts/PtlGetAllData";
import { Global } from '../../Global/index';

export default async function (call: ApiCall<ReqGetAllData, ResGetAllData>) {
    // TODO
    const source = call.req.source
    const course = call.req.course

    if(source === 'homework'){
        const ret = await Global.collection('homework').aggregate([
            {
                $match:{
                    course,
                    status:'published',
                }
            },
            {
                $sort:{
                    startTime:1
                }
            },
            {
                $lookup:{
                    from: 'userHomework',
                    localField: '_id', 
                    foreignField: 'test',
                    as: 'userHomework'
                }
            },
            {
                $lookup:{
                    from: 'questions',
                    localField: 'questions', 
                    foreignField: '_id',
                    as: 'questions'
                }
            },
            {
                $project:{
                    'course':false,
                    'startTime':false,
                    'endTime':false,
                    // 'questions':false,
                    'status':false,
                    
                    'userHomework._id':false,
                    'userHomework.student':false,
                    'userHomework.test':false,
                    'userHomework.submitTime':false,
                    'userHomework.answers':false,
                    //questions
                    'questions._id':false,
                    'questions.answer':false,
                    'questions.teacher':false,
                    'questions.question.question':false,
                    'questions.question.type':false,
                    'questions.question.questionCount':false,
                    'questions.question.selections':false,
                }
            }
        ]).toArray()

        // console.log(ret);

        call.succ({
            datas:ret
        })
    }
    else if(source === 'test'){
        const ret = await Global.collection('tests').aggregate([
            {
                $match:{
                    course,
                    status:'published',
                }
            },
            {
                $sort:{
                    startTime:1
                }
            },
            {
                $lookup:{
                    from: 'userTests',
                    localField: '_id', 
                    foreignField: 'test',
                    as: 'userHomework'
                }
            },
            {
                $lookup:{
                    from: 'questions',
                    localField: 'questions', 
                    foreignField: '_id',
                    as: 'questions'
                }
            },
            {
                $project:{
                    'course':false,
                    'startTime':false,
                    'endTime':false,
                    // 'questions':false,
                    'status':false,
                    
                    'userHomework._id':false,
                    'userHomework.student':false,
                    'userHomework.test':false,
                    'userHomework.submitTime':false,
                    'userHomework.answers':false,
                    //questions
                    'questions._id':false,
                    'questions.answer':false,
                    'questions.teacher':false,
                    'questions.question.question':false,
                    'questions.question.type':false,
                    'questions.question.questionCount':false,
                    'questions.question.selections':false,
                }
            }
        ]).toArray()

        // console.log(ret);

        call.succ({
            datas:ret
        })
    }
    else{
        const ret = await Global.collection('experiments').aggregate([
            {
                $match:{
                    course,
                }
            },
            {
                $sort:{
                    startTime:1
                }
            },
            {
                $lookup:{
                    from: 'userExperiments',
                    localField: '_id', 
                    foreignField: 'experiment',
                    as: 'userExperiments'
                }
            },
            {
                $project:{
                    'course':false,
                    'startTime':false,
                    'endTime':false,
                    'language':false,
                    'questions.description':false,
                    'questions.input':false,
                    'questions.output':false,
                    
                    'userExperiments._id':false,
                    'userExperiments.student':false,
                    'userExperiments.experiment':false,
                    'userExperiments.submitTime':false,
                    'userExperiments.answers':false,
                }
            }
        ]).toArray()

        call.succ({
            datas:ret
        })
    }
    
}